﻿using System.Web.Mvc;
using CandidateManager.Controllers;
using WI.CandidateManager.Business.Objects;

namespace CandidateManager.Areas.Candidate.Controllers
{
    public class ProfileController : AutorizeBaseController
    {
        private CandidateBO candidateBO = new CandidateBO();

        /// <summary>
        /// Views the profile.
        /// </summary>
        /// <returns></returns>
        [Authorize]
        public ActionResult ViewProfile()
        {
            return RedirectToAction("Index", "Home");
        }

        /// <summary>
        /// Changes the profile.
        /// </summary>
        /// <returns></returns>
        [Authorize]
        public ActionResult ChangeProfile()
        {
            var candidate = candidateBO.GetCandidateProfile();
            return View(candidate);
        }

        /// <summary>
        /// Changes the profile.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        public ActionResult ChangeProfile(WI.CandidateManager.Common.Data.Entities.Candidate model)
        {
            if (ModelState.IsValid)
            {
                bool result = candidateBO.Update(
                    model.FullName, 
                    model.PasswordText,
                    model.ShortName, 
                    model.Email, 
                    model.Phone, 
                    model.Gender);

                if (result)
                {
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    return RedirectToAction("ChangeProfile", "Profile");
                }
            }
            else
            {
                return RedirectToAction("ChangeProfile", "Profile");
            }
        }
    }
}
